<html>
  <head>
    <script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>
    <script type="text/javascript" src="../jsrelaxngvalidator/jssaxparser-1.2/sax.js"></script>
	<script type="text/javascript" src="../jsrelaxngvalidator/jsdatatypelibrary-0.3/datatype_library.js"></script>
	<script type="text/javascript" src="../jsrelaxngvalidator/dom_utils.js"></script>
	<script type="text/javascript" src="../jsrelaxngvalidator/applyXslt.js"></script>
	<script type="text/javascript" src="../jsrelaxngvalidator/relaxng_validator.js"></script>
	<script type="text/javascript" src="../jsrelaxngvalidator/validation_common/validator_functions.js"></script>
	<script type="text/javascript" src="../jsrelaxngvalidator/validation_common/validator_objects.js"></script>
	<script type="text/javascript" src="manageWhiteSpace.js"></script>
    <script type="text/javascript">
    
    var validation_result;
    var sax_events;
    
    function setUp() {
        validation_result = document.getElementById('validation_result');
        sax_events = document.getElementById('sax_events');  
    }
    
    function testValidateSpecTest() {
        var relaxng = createDocumentFromText(loadFile("jclark_testSuite/testSuite.rng"));
        var pattern = validate(loadFile("jclark_testSuite/spectest.xml"), relaxng);
        assertTrue(pattern.toHTML(), pattern instanceof Empty);
    }
    
    function testCorrect() {
        var spectest = createDocumentFromText(loadFile("jclark_testSuite/spectest.xml"));
        var correctSchemas = spectest.getElementsByTagName("correct");
        for (var i = 0 ; i < correctSchemas.length ; i++) {
            var correctSchema = correctSchemas.item(i);
            var testSuite = undefined;
            var parent = correctSchema;
            while (!testSuite && parent && parent.tagName) {
                parent = parent.parentNode;
                if (parent.tagName.toLowerCase() === "testsuite") {
                    testSuite = parent;
                }
            }
            validation_result.innerHTML += innerXML(testSuite) + "<br/>";
            var relaxng = undefined;
            var rootNode = getFirstChildElement(correctSchema);
            var schemaAsText = innerXML(rootNode);
            if (rootNode.tagName !== "grammar") {
                relaxng = createDocumentFromText("<rng:grammar xmlns:rng=\"http://relaxng.org/ns/structure/1.0\" datatypeLibrary=\"http://www.w3.org/2001/XMLSchema-datatypes\"><rng:start><rng:ref name=\"content\"/></rng:start><rng:define name=\"content\">" + schemaAsText + "</rng:define></rng:grammar>");
            } else {
                relaxng = createDocumentFromText(schemaAsText);
            }
            for (var next = getNextSiblingElement(correctSchema) ; next ; next = getNextSiblingElement(next)) {
                var xml = getFirstChildElement(next);
                if (next.tagName === "valid") {
                    validation_result.innerHTML += "valid :<br/>";
                    var pattern = validate(innerXML(xml), relaxng);
                    try {
                        assertTrue(pattern.toHTML() + "<br/>" + innerXML(testSuite) + "<br/>", pattern instanceof Empty);
                    } catch(e) {
                        assertTrue(e.toString() + "<br/>" + innerXML(testSuite) + "<br/>", false);
                    }
                } else if (next.tagName === "invalid") {
                    validation_result.innerHTML += "invalid :<br/>";
                    var pattern = validate(innerXML(xml), relaxng);
                    try {
                        assertTrue(pattern.toHTML() + "<br/>" + innerXML(testSuite) + "<br/>", pattern instanceof NotAllowed);
                    } catch(e) {
                        assertTrue(e.toString() + "<br/>" + innerXML(testSuite) + "<br/>", true);
                    }
                }
            }
        }
    }
    
    function validate(xml, relaxng) {
        var relaxNGValidator = new RelaxNGValidator(validation_result, sax_events, relaxng);
        var saxParser = XMLReaderFactory.createXMLReader();
        saxParser.setContentHandler(relaxNGValidator);
        saxParser.setErrorHandler(relaxNGValidator);
        relaxNGValidator.setSaxParser(saxParser);
        try {
            saxParser.parseString(xml);
        } catch(e) {
            return new NotAllowed(e.toString, e);
        }
        return relaxNGValidator.resultPattern;
    }
    
    function runAllTests () {
        //setUp();
        //testValidateSpecTest();
        setUp();
        testCorrect();
        
        alert('done!');
    }
    
    // Brett added to be able to show something in console when error is thrown
    JsUnitException.prototype.toString = function () {
        return this.jsUnitMessage;
    };

    </script>
  </head>
  <body onload="runAllTests()">
    This is a Test Page for relaxng_validator.js
		<div id="validation_result"></div>
		<div id="sax_events"></div>
  </body>
</html>
